import requests
from typing import Dict

class WeatherService:

    @staticmethod
    def get_all_weather_data(district_id: str = "420100", data_type: str = "all") -> Dict:
        """
        获取城市天气数据
        
        Args:
            district_id (str): 区域ID，默认武汉420100

        Returns:
            Dict: 包含天气数据的字典
        """
        try:
            # 天气请求的url
            url = "https://api.map.baidu.com/weather/v1/"

            # 百度地图API密钥
            ak="TWBzmMaW5GOdMDPTwBAsXXJgqcX36mWz" 

            params = {
                "district_id": district_id,
                "data_type":data_type,
                "ak": ak  # 需要替换为实际的API密钥
            }
            
            # 获取所有天气
            response = requests.get(url=url, params=params, timeout=10)

            # 处理请求异常
            response.raise_for_status() 
            now_data = response.json()
            
            return {
                    "success": True,
                    "data": now_data
                }
                
        except requests.Timeout:
            print("请求超时")
            return {
                "success": False,
                "message": "请求超时，请稍后重试"
            }
        except requests.RequestException as e:
            print(f"请求错误: {str(e)}")
            return {
                "success": False,
                "message": "网络请求失败"
            }
        except Exception as e:
            print(f"获取天气数据时发生错误: {e}")
            return {
                "success": False,
                "message": "获取天气数据失败"
            } 